﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Viettel.VOFFICE.DAO.Enums;
using Viettel.VOFFICE.Web.Helpers;
using Viettel.VOFFICE.Web.Models;

namespace Viettel.VOFFICE.Web.Controllers
{
    /// <summary>
    /// Dùng để trả về các partical view
    /// </summary>
    public class CommonController : BaseController
    {
        /// <summary>
        /// view cây phòng ban
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AttributeHelper("BookController_BuildDepartmentTreeView")]
        public ActionResult DeptTree()
        {
            var tree = unitOfWork.DeparmentRepository.DeptTree().Select(o => new DepartmentModel(o)).ToList();
            ViewBag.DeptTree = tree;
            return PartialView("../Common/DeptTree");
        }

        /// <summary>
        /// chọn user nhóm từ cây phòng ban
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AttributeHelper("BookController_BuildDeptWithUser")]
        public ActionResult DeptTreeWithUser(int? deptid)
        {
            // lấy ra tree phòng ban
            var tree = unitOfWork.DeparmentRepository.DeptTree().Select(o => new DepartmentModel(o)).ToList();
            ViewBag.DeptTree = tree;
            if (tree.Any())
            {
                ViewBag.FocusDeptId = deptid ?? tree.First().Id;
            }
            return PartialView("../Common/DeptTreeWithUser");
        }

        #region Get Dept Treeview Data
        private void SetChildren(DepartmentModel deptModel, List<DepartmentModel> deptList)
        {
            var childs = deptList.Where(s => s.ParentId == deptModel.Id).ToList();
            if (!childs.Any()) return;
            foreach (var child in childs)
            {
                SetChildren(child, deptList);
                deptModel.SubDepts.Add(child);
            }
        }
        #endregion

        /// <summary>
        /// danh sách tất cả các biểu mẫu mà người dùng đã đưa lên
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("libray_items")]
        [AttributeHelper("BookController_GetLibraryItem")]
        public ActionResult LibrayItems()
        {
            var items = unitOfWork.LibraryRepository.GetByActive(true).Select(o => new LibraryModel(o)).ToList();
            ViewBag.Items = items;
            return PartialView("../Common/LibraryItems");
        }

        /// <summary>
        /// Hiển thị đầy đủ thông tin của luồng để giúp lựa chọn tốt hơn
        /// </summary>
        /// <param name="flowId"></param>
        /// <param name="editMode"></param>
        /// <returns></returns>
        [HttpGet]
        [AttributeHelper("ShowFlowDetails")]
        [ActionName("show_flow_details")]
        public ActionResult FlowDetails(int flowId, bool editMode = false)
        {
            var flow = unitOfWork.FlowRepository.GetById(flowId);
            if (flow != null)
            {
                //ViewBag.FlowId = flow.Id;
                ViewBag.Flow = new FlowModel(flow);
                ViewBag.Nodes = flow.Nodes.OrderByDescending(o=>o.IsStart).ThenBy(o=>o.CreatedAt).Select(o => new NodeModel(o)).ToList();
                ViewBag.EditMode = editMode;
                var list = unitOfWork.FlowRepository.GetByActive().Select(o => new KeyValueModel()
                    {
                        Key = o.Id,
                        Value = o.Name
                    }).ToList();
                ViewBag.FlowList = list;
            }
            return PartialView("../Common/FlowDetails");
        }

        /// <summary>
        /// Lấy ra danh sách đơn vị đối tác
        /// </summary>
        /// <param name="allowMulti"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("select_office_partner")]
        public ActionResult SelectOfficePartner(bool allowMulti = false)
        {
            var officePartners = unitOfWork.OfficePartnerRepository.GetByActive().Select(o => new KeyValueModel()
                {
                    Key = o.Id,
                    Value = o.Name
                }).ToList();
            ViewBag.OfficePartners = officePartners;
            ViewBag.AllowMulti = allowMulti;
            return PartialView("../Common/SelectOfficePartner");
        }

        [HttpGet]
        [ActionName("controller_action_popup")]
        public ActionResult GetControllerActionPopup()
        {
            return PartialView("../Common/ControllerActionPopup");
        }
    }
}
